package com.shuziren.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shuziren.abnormal.CustomException;
import com.shuziren.config.GlobalExceptionHandler;
import com.shuziren.domain.CandidateEntity;
import com.shuziren.domain.InterPersonEntity;
import com.shuziren.domain.dto.CandidateAddDto;
import com.shuziren.domain.dto.CandidateSelectDto;
import com.shuziren.domain.dto.CandidateUpdateDto;
import com.shuziren.mapper.CandidateMapper;
import com.shuziren.service.CandidateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.GeneralSecurityException;

@Service
public class CandidateServiceImpl extends ServiceImpl<CandidateMapper,CandidateEntity> implements CandidateService {

    @Autowired
    private CandidateMapper candidateMapper;

    @Override
    public Page<CandidateEntity> selectPage(CandidateSelectDto candidateDto) {
        // 参数校验
        validateCandidateDto(candidateDto);
        candidateDto.setPageNum(candidateDto.getPageNum() == null ? 1 : candidateDto.getPageNum());
        candidateDto.setPageSize(candidateDto.getPageSize() == null ? 10 : candidateDto.getPageSize());

        CandidateEntity candidate = new CandidateEntity();
        BeanUtils.copyProperties(candidateDto, candidate);

        Page<CandidateEntity> pageParam = new Page<>(candidateDto.getPageNum(), candidateDto.getPageSize());

        QueryWrapper<CandidateEntity> candidateEntityQueryWrapper = new QueryWrapper<>();
        if (candidate.getName() != null && !candidate.getName().isBlank()){
            candidateEntityQueryWrapper.like("name", candidate.getName());
        }
        if (candidate.getInterviewStatus() != null && candidate.getInterviewStatus() != 0){

            candidateEntityQueryWrapper.eq("interview_status", candidate.getInterviewStatus());
        }



        // 执行查询
        return candidateMapper.selectPage(pageParam, candidateEntityQueryWrapper);
    }

    @Override
    public boolean addCandidate(CandidateAddDto candidateAddDto) throws CustomException {
        if (candidateAddDto != null) {
            // 将 CandidateAddDto 转换为 CandidateEntity
            CandidateEntity candidateEntity = new CandidateEntity();
            candidateEntity.setName(candidateAddDto.getName());
            candidateEntity.setGender(candidateAddDto.getGender());
            candidateEntity.setEmail(candidateAddDto.getEmail());
            candidateEntity.setPhone(candidateAddDto.getPhone());
            candidateEntity.setPosition(candidateAddDto.getPosition());
            candidateEntity.setExperience(candidateAddDto.getExperience());
            candidateEntity.setGraduateSchool(candidateAddDto.getGraduateSchool());
            candidateEntity.setSkills(candidateAddDto.getSkills());
            candidateEntity.setResumePath(candidateAddDto.getResumePath());
            candidateEntity.setNotes(candidateAddDto.getNotes());
            if (candidateAddDto.getInterviewResult() == null){
                candidateEntity.setInterviewResult(0);
            }
            if (candidateAddDto.getInterviewStatus() == null){
                candidateEntity.setInterviewStatus(1);
            }

            // 插入数据
            int result = candidateMapper.insert(candidateEntity);

            return result > 0;
        }
        return false;
    }

    @Override
    public boolean updateCandidate(CandidateUpdateDto candidateUpdateDto) {
        if (candidateUpdateDto == null || candidateUpdateDto.getId() == null) {
            throw new IllegalArgumentException("更新对象不能为空且必须包含有效的ID");
        }

        // 检查记录是否存在
        CandidateEntity existingCandidate = candidateMapper.selectById(candidateUpdateDto.getId());
        if (existingCandidate == null) {
            throw new IllegalArgumentException("要更新的记录不存在");
        }

        // 将 CandidateUpdateDto 转换为 CandidateEntity
        CandidateEntity candidateEntity = new CandidateEntity();
        candidateEntity.setId(candidateUpdateDto.getId());

        // 设置各个字段
        candidateEntity.setName(isEmpty(candidateUpdateDto.getName()) ? existingCandidate.getName() : candidateUpdateDto.getName());
        candidateEntity.setGender(candidateUpdateDto.getGender() != null ? candidateUpdateDto.getGender() : existingCandidate.getGender());
        candidateEntity.setEmail(isEmpty(candidateUpdateDto.getEmail()) ? existingCandidate.getEmail() : candidateUpdateDto.getEmail());
        candidateEntity.setPhone(isEmpty(candidateUpdateDto.getPhone()) ? existingCandidate.getPhone() : candidateUpdateDto.getPhone());
        candidateEntity.setPosition(isEmpty(candidateUpdateDto.getPosition()) ? existingCandidate.getPosition() : candidateUpdateDto.getPosition());
        candidateEntity.setExperience(candidateUpdateDto.getExperience() != null ? candidateUpdateDto.getExperience() : existingCandidate.getExperience());
        candidateEntity.setGraduateSchool(isEmpty(candidateUpdateDto.getGraduateSchool()) ? existingCandidate.getGraduateSchool() : candidateUpdateDto.getGraduateSchool());
        candidateEntity.setSkills(isEmpty(candidateUpdateDto.getSkills()) ? existingCandidate.getSkills() : candidateUpdateDto.getSkills());
        candidateEntity.setResumePath(isEmpty(candidateUpdateDto.getResumePath()) ? existingCandidate.getResumePath() : candidateUpdateDto.getResumePath());
        candidateEntity.setNotes(isEmpty(candidateUpdateDto.getNotes()) ? existingCandidate.getNotes() : candidateUpdateDto.getNotes());
        candidateEntity.setInterviewResult(candidateUpdateDto.getInterviewResult() != null ? candidateUpdateDto.getInterviewResult() : existingCandidate.getInterviewResult());
        candidateEntity.setInterviewStatus(candidateUpdateDto.getInterviewStatus() != null ? candidateUpdateDto.getInterviewStatus() : existingCandidate.getInterviewStatus());

        // 更新数据
        int result = candidateMapper.updateById(candidateEntity);
        return result > 0;
    }

    /**
     * 检查字符串是否为空或 null
     */
    private boolean isEmpty(String str) {
        return str == null || str.isEmpty();
    }



    /**
     * 校验CandidateDto参数
     */
    private void validateCandidateDto(CandidateSelectDto candidateDto) {
        if (candidateDto == null) {
            throw new IllegalArgumentException("参数不能为空");
        }
//        if (candidateDto.getPageNum() == null || candidateDto.getPageNum() < 1) {
//            throw new IllegalArgumentException("页码不能小于1");
//        }
//        if (candidateDto.getPageSize() == null || candidateDto.getPageSize() < 1){
//            throw new IllegalArgumentException("每页总数不能小于1");
//        }
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<CandidateEntity> buildQueryWrapper(CandidateSelectDto candidateDto) {
        return new LambdaQueryWrapper<CandidateEntity>()
                .like(CandidateEntity::getName, candidateDto.getName())
                .like(CandidateEntity::getInterviewStatus, candidateDto.getInterviewStatus());
    }
}
